package com.loveyukari.dao.impl;

import com.loveyukari.dao.ExchangeHistoryDao;
import com.loveyukari.model.ExchangeHistory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class JdbcExchangeHistoryDao extends BaseDao implements ExchangeHistoryDao {

    private RowMapper<ExchangeHistory> rowMapper = new RowMapper<ExchangeHistory>() {
        @Override
        public ExchangeHistory mapRow(ResultSet resultSet, int i) throws SQLException {
            ExchangeHistory exchangeHistory = new ExchangeHistory();
            exchangeHistory.setId(resultSet.getInt("id"));
            exchangeHistory.setAuthId(resultSet.getInt("auth_id"));
            exchangeHistory.setCreateTime(resultSet.getTimestamp("create_time"));
            exchangeHistory.setGfitId(resultSet.getInt("gfit_id"));
            exchangeHistory.setPrice(resultSet.getInt("price"));
            exchangeHistory.setAddressId(resultSet.getInt("address_id"));
            exchangeHistory.setExpressName(resultSet.getString("express_name"));
            exchangeHistory.setExpressNo(resultSet.getString("express_no"));
            exchangeHistory.setStatus(resultSet.getInt("status"));
            return exchangeHistory;
        }
    };

    @Override
    public void insert(ExchangeHistory exchangeHistory) {
        String sql = "insert into exchange_history (auth_id,gfit_id,price,address_id) values(?,?,?,?)";
        this.getJdbcTemplate().update(sql,exchangeHistory.getAuthId(),exchangeHistory.getGfitId(),exchangeHistory.getPrice(),exchangeHistory.getAddressId());
    }

    @Override
    public List<ExchangeHistory> selectAll() {
        String sql = "select * from exchange_history order by id desc";
        List<ExchangeHistory> list = this.getJdbcTemplate().query(sql, rowMapper);
        return list;
    }

    @Override
    public List<ExchangeHistory> selectByAuth(int authId) {
        String sql = "select * from exchange_history where auth_id = ?";
        List<ExchangeHistory> list = this.getJdbcTemplate().query(sql, rowMapper,authId);
        return list;
    }

    @Override
    public ExchangeHistory selectById(int id) {
        String sql = "select * from exchange_history where id = ?";
        List<ExchangeHistory> list = this.getJdbcTemplate().query(sql, rowMapper,id);
        if (list.isEmpty()){
            return null;
        }
        return list.get(0);
    }

    @Override
    public int sumAuthPrice(int authId) {
        String sql = "select sum(price) from exchange_history where auth_id = ?";
        Integer sum = this.getJdbcTemplate().queryForObject(sql, Integer.class, authId);
        if (sum == null){
            return 0;
        }
        return sum;
    }

    @Override
    public void updateAddress(Integer id, Integer addressId) {
        String sql = "update exchange_history set address_id = ? where id = ?";
        this.getJdbcTemplate().update(sql,addressId,id);
    }

    @Override
    public void updateExpress(Integer id, String expressNo, String expressName) {
        String sql = "update exchange_history set express_name = ? , express_no = ?, status = 1 where id = ?";
        this.getJdbcTemplate().update(sql,expressName,expressNo,id);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        String sql = "update exchange_history set status = ? where id = ?";
        this.getJdbcTemplate().update(sql,status,id);

    }
}
